﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using Intorise.Common;

namespace Model
{
    /// <summary>
    /// 服务器返回的消息
    /// </summary>
    public class Message
    {
        public MessageTypeEnum MessageType { get; set; }

        /// <summary>
        /// 数据长度
        /// </summary>
        public int Length { get; set; }
        /// <summary>
        /// 消息数据
        /// </summary>
        public byte[] Data { get; set; }
        /// <summary>
        /// 32位整数消息编码表示消息的用途
        /// </summary>
        public int Type { get; set; }
    }

    public class CurrentMapMsg : Message
    {
        public ushort ID { get; set; }
        public CurrentMapMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.CurrentMap;
            Type = 0x018706;
            this.Data = Data;
            ID = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 0));
        }
    }

    /// <summary>
    /// 进入地图
    /// </summary>
    public class MapEnteredMsg : Message
    {
        public MapEnteredMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MapEntered;
            Type = 0x00018B00;
            this.Data = Data;
            Line = Data[0] / 2;
            X = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 1));
            Y = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 3));
            Location = new Point(X / 25, Y / 25);
        }
        public short X { get; private set; }
        public short Y { get; private set; }
        public Point Location { get; private set; }

        public int Line { get; private set; }
    }

    /// <summary>
    /// 位置变化
    /// </summary>
    public class LocationChangedMsg : Message
    {
        public LocationChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.LocationChanged;
            Type = 0x00018B03;
            this.Data = Data;
            UserID = DataPackager.GetHostData64(Data, 0);
            X = DataPackager.GetHostData16(Data, 10);
            Y = DataPackager.GetHostData16(Data, 12);
            Location = new Point(X / 25, Y / 25);
        }
        public short X { get; private set; }
        public short Y { get; private set; }
        public long UserID { get; private set; }
        public Point Location { get; private set; }
    }

    /// <summary>
    /// 登陆消息
    /// </summary>
    public class LoginSuccessMsg : Message
    {
        public short Flag { get; private set; }
        /// <summary>
        /// 玩家编号
        /// </summary>
        public long ID { get; private set; }
        public string Name { get; private set; }
        public int Level { get; private set; }
        public byte[] Version { get; private set; }

        public LoginSuccessMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.LoginSuccess;
            Type = 0x00018705;
            Flag = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 0));
            ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(Data, 2));
            Version = Data.Skip(10).Take(2).ToArray();
            int length = DataPackager.GetHostData16(Data, 12);//老服11
            Name = Tools.GetString(Data, 14, length);
            //Level = DataPackager.GetHostData32(Data, 14 + length);
            var A1 = Data[Data.Length - 12];
            var A2 = Data[Data.Length - 11];
            Level = (A1 - 128) / 2 + A2 * 64;
            this.Data = Data;
        }
    }

    /// <summary>
    /// 传送返回消息
    /// </summary>
    public class TransmitMsg : Message
    {
        public Map CurrentMap { get; private set; }
        public TransmitMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.TransmitMap;
            Type = 0x00018AFD;
            this.Data = Data;
            CurrentMap = Maps.GetMap((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 1)));
        }
    }

    public class MonsterRefreshedMsg : Message
    {
        public Point Location { get; set; }
        public string Name { get; set; }
        public Map CurrentMap { get; set; }
        public long ID { get; set; }

        public MonsterRefreshedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MonsterRefreshed;
            Type = 0x0001BDBD;
            this.Data = Data;
            ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(Data, 0));
            short nameLength = 0;
            int x, y;
            //if (Data[8] == 0xA0 && Data[9] == 0x01)//地区服
            {
                nameLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 14));
                Name = Tools.GetString(Data, 16, nameLength);
                CurrentMap = Maps.GetMap((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 25 + nameLength)));
                x = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 28 + nameLength)) / 25;
                y = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 30 + nameLength)) / 25;
            }
            //else
            //{
            //    nameLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 13));
            //    Name = Tools.GetString(Data, 15, nameLength);
            //    CurrentMap = Maps.GetMap((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 23 + nameLength)));
            //    x = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 26 + nameLength)) / 25;
            //    y = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 28 + nameLength)) / 25;
            //}
            Location = new Point(x, y);
        }
    }

    public class MonsterFoundedMsg : Message
    {
        public Point Location { get; set; }
        public string Name { get; set; }
        public Map CurrentMap { get; set; }
        public long ID { get; set; }

        public MonsterFoundedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MonsterFounded;
            Type = 0x00018b05;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 6);
            short nameLength = DataPackager.GetHostData16(Data, 18);
            if (nameLength > 0)
            {
                Name = Tools.GetString(Data, 20, nameLength);
                CurrentMap = Maps.GetMap(DataPackager.GetHostData32(Data, 31 + nameLength));
            }
            int x = DataPackager.GetHostData16(Data, 35 + nameLength) / 25;
            int y = DataPackager.GetHostData16(Data, 37 + nameLength) / 25;
            Location = new Point(x, y);
        }
    }

    public class ResultMsg : Message
    {
        public string DataType { get; set; }
        public string ResultText { get; set; }
        public ResultMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.ServerResult;
            Data = data;
            //int length = DataPackager.GetHostData16(data, 0);
            //DataType = Tools.GetString(data, 2, length);
            //length = 2 + DataType.Length + 2;
            //ResultText = Tools.ConvertChar(Tools.GetString(data, length, data.Length - length));
            ResultText = Tools.ConvertChar(Tools.GetString(data));
        }
    }

    public class UnderAttackMsg : Message//怪物攻击玩家的消息
    {
        /// <summary>
        /// 攻击者编号
        /// </summary>
        public long AttackerID { get; set; }
        /// <summary>
        /// 攻击目标编号
        /// </summary>
        public long TargetID { get; set; }
        /// <summary>
        /// 伤害
        /// </summary>
        public int Damage { get; set; }
        /// <summary>
        /// 玩家当前血量
        /// </summary>
        public int CurrentHP { get; set; }

        public UnderAttackMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.Attack;
            this.Data = Data;
            TargetID = DataPackager.GetHostData64(Data, 0);
            AttackerID = DataPackager.GetHostData64(Data, 10);

            byte A1 = Data[25];
            byte A2 = Data[26];
            Damage = (A1 - 80) / 2 + (A2 * 64);

            A1 = Data[27];
            A2 = Data[28];

            CurrentHP = (A1 - 80) / 2 + (A2 * 64);
        }
    }

    public class AttackMsg : Message//玩家攻击怪物的消息
    {
        /// <summary>
        /// 攻击者编号
        /// </summary>
        public long AttackerID { get; set; }
        /// <summary>
        /// 攻击目标编号
        /// </summary>
        public long TargetID { get; set; }
        /// <summary>
        /// 伤害
        /// </summary>
        public int Damage { get; set; }
        /// <summary>
        /// 怪物当前血量
        /// </summary>
        public int CurrentHP { get; set; }

        public AttackMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.Attack;
            this.Data = Data;
            TargetID = DataPackager.GetHostData64(Data, 0);
            AttackerID = DataPackager.GetHostData64(Data, 10);
            //Damage = DataPackager.GetHostData32(Data, 21);
            //MonsterCurrentHP = DataPackager.GetHostData32(Data, 25);

            byte A1 = Data[24];
            byte A2 = Data[25];
            Damage = (A1 - 80) / 2 + (A2 * 64);

            A1 = Data[26];
            A2 = Data[27];
            if (A1 == 0 && A2 == 0)
            {
                CurrentHP = 0;
            }
            else
            {
                CurrentHP = (A1 - 80) / 2 + (A2 * 64);
            }
        }
    }

    public class MonsterMovedMsg : Message
    {
        public long MoverID { get; private set; }
        public Point Location { get; private set; }

        public MonsterMovedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MonsterMoved;
            this.Data = Data;
            MoverID = DataPackager.GetHostData64(Data, 0);
            int x = DataPackager.GetHostData16(Data, 10) / 25;//8
            int y = DataPackager.GetHostData16(Data, 12) / 25;//10
            Location = new Point(x, y);
        }
    }

    public class PlayerMovedMsg : Message
    {
        public long MoverID { get; private set; }
        public Point Location { get; private set; }
        public byte[] MoveData { get; private set; }

        public PlayerMovedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.PlayerMoved;
            this.Data = Data;
            MoverID = DataPackager.GetHostData64(Data, 0);
            int x = DataPackager.GetHostData16(Data, 10) / 25;
            int y = DataPackager.GetHostData16(Data, 12) / 25;
            Location = new Point(x, y);
            MoveData = Data.Skip(14).ToArray();
        }
    }

    public class HeathPointChangedMsg : Message
    {
        public long OwnerID { get; private set; }
        public int CurrentHP { get; private set; }
        public int TotalHP { get; private set; }

        public HeathPointChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.HeathPointChanged;
            this.Data = Data;
            OwnerID = DataPackager.GetHostData64(Data, 0);
            byte A1, A2;
            A1 = Data[Data.Length - 4];
            A2 = Data[Data.Length - 3];
            CurrentHP = (A1 - 80) / 2 + (A2 * 64);

            A1 = Data[Data.Length - 2];
            A2 = Data[Data.Length - 1];
            TotalHP = (A1 - 80) / 2 + (A2 * 64);
            //CurrentHP = DataPackager.GetHostData32(Data, 8);
            //TotalHP = DataPackager.GetHostData32(Data, 12);
        }
    }

    public class PlayerFoundedMsg : Message
    {
        public long ID { get; private set; }
        public string Name { get; private set; }
        public int Level { get; private set; }
        public int MapID { get; private set; }
        public Point Location { get; private set; }
        public int CurrentHP { get; private set; }
        public int TotalHP { get; private set; }
        public int CurrentMP { get; private set; }
        public int TotalMP { get; private set; }

        public PlayerFoundedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.PlayerFounded;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 18);
            int length = DataPackager.GetHostData32(Data, 28);
            Name = Tools.GetString(Data, 32, length);
            Level = DataPackager.GetHostData32(Data, 32 + length);
            int x = DataPackager.GetHostData16(Data, 36 + length);
            int y = DataPackager.GetHostData16(Data, 38 + length);
            Location = new Point(x, y);
            CurrentHP = DataPackager.GetHostData32(Data, 40 + length);
            TotalHP = DataPackager.GetHostData32(Data, 44 + length);
            CurrentMP = DataPackager.GetHostData32(Data, 48 + length);
            TotalMP = DataPackager.GetHostData32(Data, 52 + length);
        }
    }

    public class MpChangedMsg : Message
    {
        public long ID { get; private set; }
        public int CurrentMP { get; private set; }

        public MpChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MpChanged;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 0);
            byte a, b;
            a = Data[Data.Length - 2];
            b = Data[Data.Length - 1];
            CurrentMP = (a - 80) / 2 + (b * 64);
        }
    }

    public class HpChangedMsg : Message
    {
        public long ID { get; private set; }
        public int CurrentHP { get; private set; }

        public HpChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.HpChanged;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 0);
            byte A1, A2, A3;
            if (Data[8] == 0x1c)
            {
                A1 = Data[Data.Length - 3];
                A2 = Data[Data.Length - 2];
                A3 = Data[Data.Length - 1];
                CurrentHP = (A1 - 80) / 2 + (A2 + (A3 - 1) * 128) * 64;
            }
            else
            {
                A1 = Data[Data.Length - 2];
                A2 = Data[Data.Length - 1];
                CurrentHP = (A1 - 80) / 2 + (A2 * 64);
            }
            //CurrentHP = (ushort)DataPackager.GetHostData16(Data, 10) / 4;
        }
    }

    public class GoldChangedMsg : Message
    {
        public int GoldCount { get; private set; }
        public GoldChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.GoldChanged;
            this.Data = Data;
            byte A1 = Data[0];
            byte A2 = Data[1];
            byte A3 = Data[2];
            byte A4 = Data[3];
            GoldCount = (A1 - 80) / 2 + (A2 + (A3 - 1 + (A4 - 1) * 128) * 128) * 64;
        }
    }

    /// <summary>
    /// 获得物品
    /// </summary>
    public class ItemGetedMsg : Message
    {
        public Thing Item { get; private set; }

        public ItemGetedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.ItemGeted;
            this.Data = Data;
            long InstanceID = DataPackager.GetHostData64(Data, 0);
            var ids = Data.Skip(10).Take(3).ToList();
            ids.Insert(0, 0);
            //ids.Add(0);
            uint id = (uint)DataPackager.GetHostData32(ids.ToArray(), 0);
            Item = Thing.GetThing(id);
            if (Item == null)
            {
                Item = new Thing { ID = (uint)id, Name = "未知物品", InstanceID = InstanceID };
            }
            else
            {
                Item.InstanceID = InstanceID;
            }
        }
    }

    public class ItemDropedMsg : Message
    {
        public int Count { get; private set; }
        public Thing DropedItem { get; private set; }
        /// <summary>
        /// 所属玩家编号
        /// </summary>
        public long OwnerID { get; private set; }
        public uint TypeID { get; private set; }
        public short X { get; private set; }
        public short Y { get; private set; }

        public ItemDropedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.ItemDroped;
            this.Data = Data;

            var typeDatas = Data.Skip(10).Take(3).ToList();
            typeDatas.Insert(0, 0);
            TypeID = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);
            DropedItem = Thing.GetThing(TypeID);
            if (DropedItem == null)
            {
                DropedItem = new Thing { Name = "未知物品", ID = (uint)TypeID };
                //if (TypeID > 1000000)//分类编号大于100万为卓越物品
                //{
                //    DropedItem.Name = "卓越装备";
                //}
            }
            DropedItem.InstanceID = DataPackager.GetHostData64(Data, 0);
            Count = DataPackager.GetHostData32(Data, 15);
            OwnerID = DataPackager.GetHostData64(Data, Data.Length - 26);
            short length = DataPackager.GetHostData16(Data, 16);
            X = DataPackager.GetHostData16(Data, Data.Length - 6);
            Y = DataPackager.GetHostData16(Data, Data.Length - 4);
        }
    }

    public class RideMsg : Message
    {
        public bool IsRide { get; private set; }
        public long PlayerID { get; private set; }

        public RideMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.Ride;
            this.Data = Data;
            PlayerID = DataPackager.GetHostData64(Data, 0);
            IsRide = BitConverter.ToBoolean(Data, 12);
        }
    }

    /// <summary>
    /// 捡到物品
    /// </summary>
    public class ItemPickupedMsg : Message
    {
        public uint TypeID { get; private set; }
        public long InstanceID { get; private set; }
        public Thing GetedItem { get; private set; }

        public ItemPickupedMsg(byte[] Data)
        {
            //00 01 97 14 84 D9 E5 BC B6 D3 AA F9 A0 01 86 BA 55 00 00 BA 03 
            MessageType = MessageTypeEnum.ItemPickuped;
            this.Data = Data;
            var typeDatas = Data.Skip(10).Take(3).ToList();
            typeDatas.Insert(0, 0);
            TypeID = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);

            InstanceID = DataPackager.GetHostData64(Data, 0);
            GetedItem = Thing.GetThing(TypeID);
            if (GetedItem == null)
            {
                GetedItem = new Thing { Name = "未知物品", ID = (uint)TypeID };
                //if (TypeID > 1000000)//分类编号大于100万为卓越装备
                //{
                //    GetedItem.Name = "卓越装备";
                //}
            }
            GetedItem.InstanceID = InstanceID;
        }
    }

    public class PickupItemMsg : Message
    {
        public string Name { get; private set; }

        public PickupItemMsg(byte[] Data)
        {
            this.Data = Data;
            int length = DataPackager.GetHostData16(Data, 18);
            Name = Tools.GetString(Data, 20, length);
        }
    }

    public class SignCountMsg : Message
    {
        public int Count { get; set; }
        public SignCountMsg(byte[] data)
        {
            Count = DataPackager.GetHostData16(data, 0);
        }
    }

    public class CleanBackpackMsg : Message
    {
          /// <summary>
        /// 当前账号
        /// </summary>
        /// <param name="player"></param>
        public CleanBackpackMsg(Player player)
        {
            this.Player = player;
        }

        /// <summary>
        /// 当前账号
        /// </summary>
        public Player Player { get; set; }
        public long[] InstanceID { get; private set; }
        public uint[] TypeID { get; private set; }
        public Thing[] GetedItem { get; private set; }
        int i;
        public int ThingNum { get; private set; }
        //public int []ThingCount { get; private set; }
        public byte[] Versions { get; private set; }
        public CleanBackpackMsg(byte[] Data ,Player player)
        {
            var typeDatas = Data.Skip(10).Take(3).ToList();
            ThingNum = 0;
           
            InstanceID = new long[100];
            TypeID = new uint[100];
            GetedItem = new Thing[100];
           // ThingCount = new int[100];
            int j = 0;
            for (i = 0; i < Data.Length - 10; i++)
            {
                Versions = Data.Skip(i).Take(2).ToArray();
                if (Versions[0] == player.Version[0] && Versions[1] == player.Version[1])
              //  if ( Versions[0]>162 &&  Versions[0]<169 && Versions[1] == Player.Version[1])
                {
                    if (Data[i + 7] == 0 || Data[i + 7] == 1)
                        ThingNum = Data[i + 6] / 2 - 1;
                    else
                        ThingNum = Data[i + 7] / 2 - 1;
                    

                    InstanceID[ThingNum] = DataPackager.GetHostData64(Data, i - 8);
                    typeDatas = Data.Skip(i + 2).Take(3).ToList();
                    
                    typeDatas.Insert(0, 0);
                    TypeID[ThingNum] = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);
                    GetedItem[ThingNum] = Thing.GetThing(TypeID[ThingNum]);
                    if (GetedItem[ThingNum] == null)
                    {
                        GetedItem[ThingNum] = new Thing { Name = "未知物品", ID = (uint)TypeID[ThingNum] };

                    }

                    int a1 = Data[i + 5];
                    int a2 = Data[i + 6];
                    if (Data[i + 7] == 0)
                    {

                        GetedItem[ThingNum].Count = a1 / 2;

                    }
                    else
                        GetedItem[ThingNum].Count = (a1 - 128) / 2 + a2 * 64;
                    GetedItem[ThingNum].InstanceID = InstanceID[ThingNum];
                    j++;
                    i = i + 8;
                }
                ThingNum = j;
            }
        }

    }

    public class DiamondsMsg : Message
    {

        public int Num { get; set; }
        public DiamondsMsg(byte[] data)
        {
            int a1 = data[0];
            int a2 = 0;
            if (data.Length > 1)
                a2 = data[1];

            if (a2 == 0)
                Num = a1 / 2;
            else
                Num = (a1 - 128) / 2 + a2 * 64;
            
        }
    }


    public class LevelUpMsg : Message
    {
        public int Level { get; set; }
        public LevelUpMsg(byte[] data)
        {
            var A1 = data[data.Length - 2];
            var A2 = data[data.Length - 1];
            Level = (A1 - 80) / 2 + (A2 * 64);
        }
    }

    #region 交易数据包分析
    /// <summary>
    /// 收到交易请求
    /// </summary>
    public class InviteTradeMsg : Message
    {
        /// <summary>
        /// 邀请交易的玩家编号
        /// </summary>
        public long InvitePlayerID { get; private set; }

        public byte[] Version { get; private set; }

        public InviteTradeMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.InviteTrade;
            this.Data = data;
            InvitePlayerID = DataPackager.GetHostData64(Data, Data.Length - 10);
            Version = Data.Skip(Data.Length-2).Take(2).ToArray();

        }
    }

    /// <summary>
    /// 同意交易
    /// </summary>
    public class AgreeTradeMsg : Message
    {

        public AgreeTradeMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.AgreeTrade;
            this.Data = data;
        }
    }

    /// <summary>
    /// 交易锁定
    /// </summary>
    public class LockTradeMsg : Message
    {
        /// <summary>
        /// 锁定交易的玩家编号
        /// </summary>
        public long InvitePlayerID { get; private set; }

        public LockTradeMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.LockTrade;
            this.Data = data;
            InvitePlayerID = DataPackager.GetHostData64(Data, 0);
        }
    }

    /// <summary>
    /// 交易确认
    /// </summary>
    //public class AffirmTradeMsg : Message
    //{
    //    /// <summary>
    //    /// 确认交易的玩家编号
    //    /// </summary>
    //    public long InvitePlayerID { get; private set; }

    //    public AffirmTradeMsg(byte[] data)
    //    {
    //        MessageType = MessageTypeEnum.AffirmTrade;
    //        this.Data = data;
    //        InvitePlayerID = DataPackager.GetHostData64(Data, 0);
    //    }
    //}
    #endregion

    public class UnloadGoodsMsg : Message
    {
        /// <summary>
        /// 锁定交易的玩家编号
        /// </summary>
        public long PlayerID { get; private set; }
        public long[] ThingID { get; private set; }
        public int ThingNum { get; private set; }
        public byte[] ThingVersion { get; private set; }
        public UnloadGoodsMsg(byte[] data, long ID)
        {
            MessageType = MessageTypeEnum.UnloadGoods;
            this.Data = data;
            ThingNum = 0;
            ThingID = new long[20];
            ThingVersion = new byte[20];


            for (int i = 0; i < data.Length - 10; i++)
            {
                PlayerID = DataPackager.GetHostData64(Data, i);
                if (PlayerID == ID)
                {
                    ThingID[ThingNum] = DataPackager.GetHostData64(Data, i - 9);
                    ThingVersion[ThingNum] = data[i - 1];
                    ThingNum++;
                }
            }



        }
    }

    /// <summary>
    /// 搜索市场返回的物品挂卖数据，包括卖家，物品，数量，单价
    /// </summary>
    public class SearchMarketHouseResultMsg : Message
    {
        /// <summary>
        /// 当前账号
        /// </summary>
        /// <param name="player"></param>
        public SearchMarketHouseResultMsg(Player player)
        {
            this.Player = player;
        }

        /// <summary>
        /// 当前账号
        /// </summary>
        public Player Player { get; set; }
        /// <summary>
        /// 挂卖者的编号
        /// </summary>
        public long PlayerID { get; set; }

        /// <summary>
        /// 挂卖的物品编号
        /// </summary>
        public long ItemID { get; private set; }
        
        /// <summary>
        /// 挂卖的物品名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 挂卖的最低价格
        /// </summary>
        public decimal FloorPrice { get; set; }

        /// <summary>
        /// 最低价格挂卖的数量
        /// </summary>


        public double Price { get; set; }

        public double Count { get; set; }

        //public double Prices { get; set; }

        public SearchMarketHouseResultMsg(byte[] data, Player player)
        {
            //分析数据获取市场里卖的物品的数量及单价等信息

            int Num = 0;

           // double[] Prices = new double[100];

           // Prices[0] = Player.FloorPrice;

            for (int i = 30; i < data.Length - 25; i++)
            {
                ItemID = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, i));
                if (ItemID == player.GemID)
                
                {
                    if (data[i + 21] == 159 && data[i + 22] == 73)
                    {
                        Price = data[i + 9] / 2;
                        if (data[i + 24] == 0)
                            Count = data[i + 23] / 2;
                        else
                            Count = (data[i + 23] - 128) / 2 + data[i + 24] * 64;
                    }
                    else if (data[i + 22] == 159 && data[i + 23] == 73)
                    {
                        Price = (data[i + 9] - 128) / 2 + data[i + 10] * 64;
                        if (data[i + 25] == 0)
                            Count = data[i + 24] / 2;
                        else
                            Count = (data[i + 24] - 128) / 2 + data[i + 25] * 64;

                    }
                    else
                        continue;

                    if (Count < 10) //数量低于10直接返回
                        continue;

                   // Prices[Num] = Price / Count;
                    if (player.FloorPrice > Price / Count)
                        player.FloorPrice = Price / Count;
                    Num++;
                }

            }
            //double k = 0;
            //for (int i = 0; i < Num-1; i++)
            //{
            //    for (int j = 0; j < Num - i - 1; j++)
            //    {
            //        if (Prices[j] > Prices[j + 1])
            //        {
            //            k = Prices[j];
            //            Prices[j] = Prices[j + 1];
            //            Prices[j + 1] = k;
            //        }
            //    }
            //}
            //if (Player.GemID == -7240129340360908877 || Player.GemID == -6444680003607617613) //玛雅或者祝福
            //    Player.FloorPrice = Prices[0]; //倒数第一位的价格
            //else
            //{
            //    if (Num > 1)
            //        Player.FloorPrice = Prices[1]; //倒数第二位的价格
            //}
           

        }
    }
}


